$(function (e) {
	//正面身份证图片上传
	$("#uploadzhengForm").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: imgurl,
            type: "POST",
            data:  new FormData(this),
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            contentType: false,
            cache: false,
            processData: false,
            // 显示加载图片
            beforeSend: function () {
                $('.loading-shadow').addClass('active');
            },
            success: function (data) {
                $('#img1').attr('src',data.msg);
                $('#idcardimg').attr('value',data.msg);
            },
            error: function(){}
        });
    });
    //反面身份证图片上传
    $("#uploadfanForm").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: imgurl,
            type: "POST",
            data:  new FormData(this),
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            contentType: false,
            cache: false,
            processData: false,
            // 显示加载图片
            beforeSend: function () {
                $('.loading-shadow').addClass('active');
            },
            success: function (data) {
                $('#img2').attr('src',data.msg);
                $('#idcardimgf').attr('value',data.msg);
            },
            error: function(){}
        });
    });
});
$(function(){
    // 认证页面上传图片限制大小
    var frontBox = document.querySelector("#frontBox");
    var front_icon = document.querySelector("#front_icon");
    frontBox.onclick = function(){
        front_icon.click();     //file1触发事件
    }
    var myFile;
    front_icon.onchange = function(){     //当事件改变的时候触发
        $("#uploadzhengForm").trigger('submit'); 
        // 获取上传文件的大小
        // var file_size=parseInt((document.getElementById('front_icon').files[0].size)/1024/1024)
        // if(file_size <= 2) {
            // $("#uploadzhengForm").trigger('submit');
        // }else{
        //     msg('图片大小不能超过2M!');
        // }
    }
   //认证页面上传图片限制大小
    var backBox = document.querySelector("#backBox");
    var back_icon = document.querySelector("#back_icon")   
    backBox.onclick = function(){
        back_icon.click();     //file1触发事件
    }
    var myFile;
    back_icon.onchange = function(){     //当事件改变的时候触发
         $("#uploadfanForm").trigger('submit');
        // var file_size=parseInt((document.getElementById('back_icon').files[0].size)/1024/1024)
        // if(file_size <= 2) {
        //     $("#uploadfanForm").trigger('submit');
        // }else{
        //     msg('图片大小不能超过2M!');
        // }
    }
})
//验证身份证号
function isCreditNo(vStr){
    var vCity = [
        '11','12','13','14','15','21','22',
        '23','31','32','33','34','35','36',
        '37','41','42','43','44','45','46',
        '50','51','52','53','54','61','62',
        '63','64','65','71','81','82','91'
    ];
    var reg = /^([\d]{17}[xX\d]|[\d]{15})$/;
    if (!(reg.test(vStr))) return false;
    if (!$.inArray(vStr.substr(0, 2), vCity)) return false;
    vStr = vStr.replace(/[xX]$/i, 'a');
    var vLength = vStr.length;
    var vBirthday;
    if (vLength == 18) {
        vBirthday = vStr.substr(6, 4) +'-' +vStr.substr(10, 2) + '-' + vStr.substr(12, 2);
    } else {
        vBirthday = '19' + vStr.substr(6, 2) + '-' + vStr.substr(8, 2) + '-' + vStr.substr(10, 2);
    }
    //if (date('Y-m-d', strtotime(vBirthday)) != vBirthday) return false;
    if (vLength == 18) {
        var vSum = 0;
        for ( var i = 17 ; i >= 0 ; i--) {
            var vSubStr = vStr.substr(17 - i, 1);
            vSum += ( Math.pow(2, i) % 11) * ((vSubStr == 'a') ? 10 : parseInt(vSubStr , 11));
        }
        if(vSum % 11 != 1) return false;
    }
    return true;
}
//确认按钮  提交信息
function confirm(){
	//获取姓名，身份证号，身份证照片，学历，毕业院校
	var fname      = $.trim($('#fname').val());
	var fid        = $.trim($('#fid').val());
	var frontimg   = $.trim($('input[name=idcardimg]').val());
	var backimg  = $.trim($('input[name=idcardimgf]').val());
	var fedu       = $.trim($('#fedu').val());
	var fschool    = $.trim($('#fschool').val());
	//验证中文名，身份证号
	var isChinaName=/^[\u4E00-\uFA29]|[\uE7C7-\uE7F3]{2,6}$/;
	var isCHName=isChinaName.test(fname);
	var isIDnum = isCreditNo(fid);
	if(fname == '' || !isCHName) {
		msg('请输入正确的中文姓名');
		return;
	}else if(fid == '' || !isIDnum) {
		msg('请输入正确的身份证号');
		return;
	}else if(frontimg == '' || backimg == ''){
		msg('请上传身份证');
		return;
	}
//	else if(fedu == '') {
//		msg('请输入学历');
//		return;
//	}else if(fschool == '') {
//		msg('请输入毕业院校');
//		return;
//	}
	$.ajax({
        url:schoolurl,
        type: 'post',
        data: {'diplomas':fedu,'school':fschool},
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
        dataType:'json',
        success: function(res){
        }
    });
	$.ajax({
        url: cardauth,
        type: 'post',
        data: {'idcardimg':frontimg,'idcardimgf':backimg,'username':fname,'idcard':fid},
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
        dataType:'json',
        success: function(res){
            if(res.status == 1){
               msg('请输入正确的身份证号');
			   return;
            }else if(res.status == 0){
                //第一次提交成功
                window.location.reload();
            }else if(res.status == 3){
                //修改成功
                window.location.reload();
            }
        }
    });
    $('#fillin').hide();
    $('#ingtip').show();
}
//重新填写  填写信息表展示  审核中提示图片隐藏
function updateauth(){
	$('#fillin').show();
	$('#ingtip').hide();
	$('#errortip').hide();
}
